package com.my.cd.compose;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Predicate;

/**
 * 服务员类：菜单使用者
 */
public class Waitress {

    private ArrayList<MenuComponent> menuItems = new ArrayList<>();

    public Waitress() {
    }

    public void addComponent(MenuComponent mMenuComponent) {
        menuItems.add(mMenuComponent);
    }

    public void printMenu() {
        Iterator iterator;
        MenuComponent menuItem;
        for (MenuComponent item : menuItems) {
            item.print();
            iterator = item.getIterator();
            while (iterator.hasNext()) {
                menuItem = (MenuComponent) iterator.next();
                menuItem.print();
            }
        }
    }

    /**
     * 带过滤展示菜单
     */
    public void printMenu(Predicate<MenuComponent> predicate) {
        Iterator iterator;
        MenuComponent menuItem;
        for (MenuComponent item : menuItems) {
            if (predicate.test(item)) {
                item.print();
            }
            iterator = item.getIterator();
            while (iterator.hasNext()) {
                menuItem = (MenuComponent) iterator.next();
                if (predicate.test(menuItem)) {
                    menuItem.print();
                }
            }
        }
    }

}
